package com.gruopbuying.agent.controller;

import com.gruopbuying.agent.annotation.Log;
import com.gruopbuying.agent.context.FilterContextHandler;
import com.gruopbuying.agent.domain.RoleDO;
import com.gruopbuying.agent.domain.SysAgentInfoDO;
import com.gruopbuying.agent.dto.LoginUserDTO;
import com.gruopbuying.agent.dto.UserDTO;
import com.gruopbuying.agent.dto.do2dto.UserConvert;
import com.gruopbuying.agent.service.RoleService;
import com.gruopbuying.agent.service.SysAgentInfoService;
import com.gruopbuying.agent.service.UserService;
import com.gruopbuying.agent.utils.PageUtils;
import com.gruopbuying.agent.utils.Query;
import com.gruopbuying.agent.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户信息
 * @author groupbuying
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
    RoleService roleService;
	@Autowired
    SysAgentInfoService sysAgentInfoService;
	@Autowired
    UserService userService;



	/**
	 * 登录的当前用户，前台需要验证用户登录的页面可以调用此方法
	 * @return
	 */
    @GetMapping("/currentUser")
	public LoginUserDTO currentUser(){
		LoginUserDTO loginUserDTO = new LoginUserDTO();
		Long roleId = roleService.roleIdsByUserId(Long.parseLong(FilterContextHandler.getUserID())).get(0);

		SysAgentInfoDO sysAgentInfoDO = sysAgentInfoService.get(FilterContextHandler.getAgentId());
		if(sysAgentInfoDO == null || sysAgentInfoDO.getId().equals("00000")){
			//总代理商
			RoleDO roleDO = roleService.get(roleId);
			loginUserDTO.setUserId(FilterContextHandler.getUserID());
			loginUserDTO.setUsername(FilterContextHandler.getUsername());
			//System.out.println(roleDO.getRoleName());
			loginUserDTO.setName("(" + roleDO.getRoleName() + ")" + FilterContextHandler.getName());
			return loginUserDTO;
		} else {
			loginUserDTO.setUserId(FilterContextHandler.getUserID());
			loginUserDTO.setUsername(FilterContextHandler.getUsername());
			loginUserDTO.setName(sysAgentInfoDO.getAgentName());
			return loginUserDTO;
		}
	}

	/**
	 * 根据用户id获取用户
	 * @param id
	 * @return
	 */
	@GetMapping("{id}")
    R get(@PathVariable("id") Long id ){
		UserDTO userDTO = UserConvert.MAPPER.do2dto(userService.get(id));
		return R.ok().put("data",userDTO);
	}

	/**
	 * 分页查询用户
	 * @param params
	 * @return
	 */
	@Log("获取用户列表")
	@GetMapping()
	public R listByPage(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		List<UserDTO> userDTOS = UserConvert.MAPPER.dos2dtos((userService.list(query)));

		for(UserDTO userDTO : userDTOS){
			Long roleId = roleService.roleIdsByUserId(userDTO.getUserId()).get(0);
			RoleDO roleDO = roleService.get(roleId);
			userDTO.setRoleName(roleDO.getRoleName());
		}

		Long roleId = roleService.roleIdsByUserId(Long.parseLong(FilterContextHandler.getUserID())).get(0);
		RoleDO roleDO = roleService.get(roleId);


		int total = userService.count(query);
		PageUtils pageUtil = new PageUtils(userDTOS, total);
		return R.ok().put("page",pageUtil);
	}


	@PostMapping("/exits")
	@ResponseBody
	boolean exits(@RequestParam Map<String, Object> params) {
		// 存在，不通过，false
		return !userService.exits(params);
	}

	@RequestMapping("/lists")
    R listsByPage(@RequestParam Map<String, Object> params) {
		System.out.println("------请求列表-------------");
		Query query = new Query(params);
		List<UserDTO> userDTOS = UserConvert.MAPPER.dos2dtos((userService.list(query)));
		int total = userService.count(query);
		PageUtils pageUtil = new PageUtils(userDTOS, total);
		return R.ok().put("page",pageUtil);
	}


}
